const router = require('koa-router')()
const service = require('../service/projectService')

router.get('/project/createProject', async (ctx, next) => {
  await next();
  await service.createProject()
})

router.get('/project/:project/pages', async (ctx, next) => {
  await next()
  try {
    const pages = await service.getProjectPages(ctx.params.project)
    ctx.body = {
      success: true,
      data: pages,
      message: ''
    }
  } catch (error) {
    ctx.body = {
      success: false,
      data: '',
      message: error
    }
  }
})

router.post('/project/asyncConfig', async (ctx, next) => {
  await next();
  const {
    project,
    page,
    pageConfig
  } = JSON.parse(ctx.request.body);

  const result = await service.asyncConfig({
    project,
    page,
    pageConfig
  });
  ctx.body = result;
})

router.post('/project/getPageConfig', async (ctx, next) => {
  await next()
  const {
    project,
    page
  } = JSON.parse(ctx.request.body)
  try {
    const config = await service.getPageConfig(project, page);
    ctx.body = {
      success: true,
      data: config,
      message: ''
    }
  } catch (error) {
    ctx.body = {
      success: false,
      data: '',
      message: error,
    }
  }
})

router.post('/project/getPageCode', async (ctx, next) => {
  await next();
  const {
    project,
    page
  } = JSON.parse(ctx.request.body);
  const code = await service.getCode(project, page);
  ctx.body = {
    success: true,
    data: code,
    message: '获取代码成功'
  }
});

router.post('/project/asyncCode', async (ctx, next) => {
  const { project, page, code } = JSON.parse(ctx.request.body);
  ctx.body = service.asyncCode({ project, page, code });
});



module.exports = router